#include "clock.h"
#include "ui_clock.h"

#include<Qpainter>
#include<Qpen>
#include<QTime>
#include<QTimer>
#include<QLabel>
#include<QPushButton>
#include<QLCDNumber>
#include<QMouseEvent>
#include <QTextToSpeech>
clock::clock(QMainWindow *parent)
    : QMainWindow(parent)
    , ui(new Ui::clock)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);//禁止用户点击窗口最大化按钮
    resize(600,600);//设置窗口大小
    QTimer *timer=new QTimer(this);//产生一个定时器
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));//关联定时器的信号与槽
    timer->start(1000);//启动定时器，每一秒钟更新一次

    //this->resize(300,100);
    this->setWindowFlag(Qt::FramelessWindowHint);//去掉标题
    QPalette p = this->palette();//背景颜色
    p.setColor(QPalette::Window,Qt::white);//设置颜色
    this->setPalette(p);
    this->setWindowOpacity(1.0);//透明度

    timer_id = startTimer(1000);//启用定时，1秒发一次信号

    //时间显示属性设置
    text_LCD = new QLCDNumber(this);
    text_LCD->resize(150,50);//大小
    text_LCD->move(225,400);

    //备忘录属性设置
    text_edit = new QTextEdit(this);
    text_edit->resize(200,40);//大小
    text_edit->move(200,460);//位置

    //说明按钮属性设置
    ui->pushButton_shuoming->setText("说明");//名字
    ui->pushButton_shuoming->resize(40,30);//大小
    ui->pushButton_shuoming->move(width()-455,430);//位置


    //设置按钮属性设置
    setting_btn = new QPushButton(this);
    setting_btn->setText("设置");//名字
    setting_btn->setObjectName("shezhi");
    setting_btn->resize(75,30);//大小
    setting_btn->move(width()-455,400);//位置

    //启动按钮属性设置
    start_btn = new QPushButton(this);
    start_btn->setText("启动");
    start_btn->resize(75,30);
    start_btn->move(width()-225,400);

    //停止按钮属性设置
    stop_btn = new QPushButton(this);
    stop_btn->setText("停止");
    stop_btn->resize(75,30);
    stop_btn->move(width()-225,430);

    //语音播报属性设置
    QTextToSpeech *speech;
    speech=new QTextToSpeech(this);
    speech->setRate(0.0);
    speech->setPitch(1.0);
    speech->setVolume(1.0);

    //连接到对应槽函数
    connect(setting_btn, &QPushButton::clicked, this, &clock::on_setting_btn_clicked);
    connect(start_btn, &QPushButton::clicked, this, &clock::on_start_btn_clicked);
    connect(stop_btn, &QPushButton::clicked, this, &clock::on_stop_btn_clicked);
    connect(this, &clock::signal_1, [&](){
        QMessageBox::information(this, "提示","设置完成",QMessageBox::Yes); });


}

clock::~clock()
{
    delete ui;
}
void clock::paintEvent(QPaintEvent *)//绘制时钟函数
{
    QPainter painter(this);
    painter.drawPixmap(0,0,width(),height(),QPixmap(":/_1.jpg"));
    static const QPoint hour[3]={
        QPoint(14,15),
        QPoint(-14,15),
        QPoint(0,-110)
    };
    static const QPoint minute[3]={
        QPoint(11,13),
        QPoint(-11,13),
        QPoint(0,-170)
    };
    static const QPoint second[3]={
        QPoint(7,8),
        QPoint(-7,8),
        QPoint(0,-210)
    };

    int size=qMin(width(),height());//获取宽度和高度
    QTime time=QTime::currentTime();//获取系统当前时间

    QPainter p(this);//创建画布对象
    p.setRenderHint(QPainter::Antialiasing);//防止图形形变
    p.translate(width()/2,height()/2);//平移坐标系中心
    p.scale(size/600.0,size/600.0);//缩放

    QBrush brush;//定义画刷
    brush.setColor(QColor(245,182,95));//设置画刷颜色
    brush.setStyle(Qt::SolidPattern);//设置样式

    QPen pen;//定义画笔
    pen.setWidth(10);//设置画笔宽度
    pen.setColor(QColor(205,214,216));//设置画刷颜色
    pen.setStyle(Qt::SolidLine);//设置样式
    p.setPen(pen);//把画笔交给画家

    p.drawEllipse(QPoint(0,0),280,280);//外圆
    //pen.setColor(QColor(255,0,0));//设置中间颜色

    //绘制时针
    p.setBrush(brush);//把画刷交给画家
    p.setPen(Qt::NoPen);
    p.setBrush(QColor(119,217,176));
    p.save();//保存当前状态

    //图形旋转
    p.rotate(30.0*(time.hour()+time.minute()/60.0));
    p.drawConvexPolygon(hour,3);
    p.restore();

    //绘制小时线
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(5);
    pen.setColor(Qt::black);
    p.setPen(pen);

    for(int i=0;i<12;i++)
    {
        p.drawLine(0,268,0,275);//画小时线
        p.drawText(-5,-235,QString::number(i));//表明小时数
        p.rotate(30);//每画一次旋转30度
    }

    //绘制分针
    p.setPen(Qt::NoPen);
    p.setBrush(QColor(144,199,247));
    p.save();
    p.rotate(6.0*(time.minute()+time.second()/60.0));//顺时针旋转到秒针的位置
    p.drawConvexPolygon(minute,3);
    p.restore();

    //绘制分针线
    pen.setStyle(Qt::SolidLine);
    pen.setWidth(1);
    pen.setColor(QColor(0,0,0));
    p.setPen(pen);
    for(int i=0;i<60;i++)
    {
        if((i%5)!=0)
            p.drawLine(0,265,0,276);
        p.rotate(6);
    }

    //绘制秒针
    p.setPen(Qt::NoPen);
    p.setBrush(QColor(120,210,220));
    p.save();
    p.rotate(6*time.second());//顺时针旋转到秒针的位置
    p.drawConvexPolygon(second,3);
    p.restore();

    //绘制动态时钟上午下午
    p.setPen(Qt::black);
    if(time.hour()>=12)
        p.drawText(-7,50,"PM");//绘制文本区
    else
        p.drawText(-7,50,"AM");//绘制文本区
}

void clock::on_setting_btn_clicked()
{
    if((++time_setting)%2 == 1)//奇数次按下设置闹钟时间，偶数次按下，设置完成
    {
        killTimer(timer_id);//关闭定时
        char time[6];//中间变量
        sprintf(time, "%d:%d",hour, min);//拼接字符串
        QString set_time(time);//char*转换成QString类型
        text_LCD->display(time);//显示设置闹钟时间时间
    }else
    {
        emit signal_1();//设置完成，发射信号
        timer_id = startTimer(1000);//启用定时
    }
}


void clock::on_start_btn_clicked()
{
    if(time_setting%2 == 1)//奇数次设置时间，偶数次不设置
    {
        hour =  (hour+1)%24;//时加1
        //显示时间
        char time[6];
        sprintf(time, "%d:%d",hour, min);
        QString set_time(time);
        text_LCD->display(set_time);
    }else
    {
        say_flag = true;//允许播报
        start_btn->setEnabled(false);//启动定时后此按键不可用
    }
}

void clock::on_stop_btn_clicked()
{
    if(time_setting%2 == 1)//奇数设置时间，偶数次不设置
    {
        min = (min+1)%60;//分加1
        //显示时间
        char time[6];
        sprintf(time, "%d:%d",hour, min);
        QString set_time(time);
        text_LCD->display(set_time);
    }else
    {
        say_flag = false;//禁止播报
        start_btn->setEnabled(true);//禁止播报后允许重新使用启动
    }
}


//检查当前时间是否达到闹钟设定时间
void clock::timerEvent(QTimerEvent *)
{
    QTime systime=QTime::currentTime();
    QString timeText =systime.toString("hh:mm");//转换成Qstring类型
    char time[6];
    sprintf(time,"%02d:%02d",hour, min);//响铃时间
    QString set_time(time);//char*转换成astring类型
    if(timeText == set_time)//当前时间与响铃时间一致
    {
        if(say_flag)//是否允许播报
        {
            speech.say(text_edit->toPlainText());//开始播报内容
        }
    }

    //显示时间(是否显示秒':')
    if(showflag)
    {
        timeText[2]=':';
        showflag=false;
    }else
    {
        timeText[2]=' ';
        showflag=true;
    }
    text_LCD->display(timeText);
}

//重写鼠标按下事件
void clock::mousePressEvent(QMouseEvent *e)
{
    if(e->button()==Qt::LeftButton) //左键按下可拖拽
    {
        movePoint=e->globalPos()-this->frameGeometry().topLeft();
    }
    if(e->button()== Qt::RightButton)
    {
        this->close();
    }
}

//重写鼠标移动事件
 void clock::mouseMoveEvent (QMouseEvent *e)
{
     if(e->buttons() ==Qt::LeftButton)
     {
         this->move(e->globalPos()- movePoint);
     }
}

void clock::childwindow_close()
{
    childwindow_ui=nullptr;
    this->setEnabled(true);
}


void clock::on_pushButton_shuoming_clicked()
{
    if(!childwindow_ui)
    {
        this->setEnabled(false);
        childwindow_ui = new childWindow(this);
        connect(childwindow_ui,&childWindow::closed,this,&clock::childwindow_close);
        childwindow_ui -> show();
    }
}
